package com.zsck.pos.base;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.chinaums.commoncheck.R;
import com.zsck.pos.common.ClickListenerProxy;


public abstract class BaseTitleActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTv_title;
    private FrameLayout mFl_base;
    private RelativeLayout mRl_back;
    private Activity mActivity = null;
    protected RelativeLayout mRl_right;
    protected ImageView mIv_left;
    private ImageView mIv_right;
    private TextView mTv_right;
    public Toolbar mToolbar;

    public abstract int setLayout();
    public abstract String setTitle();
    public abstract Activity setActivity();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_title);
        init();
    }

    private void init() {
        mActivity = setActivity();

//        RelativeLayout ll_top = findViewById(R.id.ll_top);
//        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_top.getLayoutParams();
//        params.topMargin = Constants.TOP_STATUS_H;
//        ll_top.setLayoutParams(params);
        mTv_title = findViewById(R.id.tv_title);
        mFl_base = findViewById(R.id.fl_base);
        mRl_back = findViewById(R.id.rl_back);
        mRl_right = findViewById(R.id.rl_right);
        mIv_right = findViewById(R.id.iv_right);
        mIv_left = findViewById(R.id.iv_left);
        mTv_right = findViewById(R.id.tv_right);
        mToolbar = findViewById(R.id.toolbar);
        mTv_title.setText(setTitle());
        mRl_back.setOnClickListener(this);
        mFl_base.removeAllViews();
        mFl_base.addView(LayoutInflater.from(mActivity).inflate(setLayout(), mFl_base,false));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_back:
                if (mActivity != null) {
                    mActivity.finish();
                }
                break;
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public interface RightClickListener {
        void onRightClick();
    }
    public void setRightTitle(int drawableId, final RightClickListener listener){
        mRl_right.setVisibility(View.VISIBLE);
        mIv_right.setVisibility(View.VISIBLE);
        mTv_right.setVisibility(View.GONE);
        mIv_right.setBackgroundResource(drawableId);
        mRl_right.setOnClickListener(ClickListenerProxy.getProxy(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onRightClick();
            }
        }));
    }
    public void setRightTitle(String right, final RightClickListener listener){
        mRl_right.setVisibility(View.VISIBLE);
        mIv_right.setVisibility(View.GONE);
        mTv_right.setVisibility(View.VISIBLE);
        mTv_right.setText(right);
        mRl_right.setOnClickListener(ClickListenerProxy.getProxy(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onRightClick();
            }
        }));
    }

    public void changeTitle(String title) {
        mTv_title.setText(title);
    }


    public void setShareVisibility(boolean visibility){
        mIv_right.setVisibility(visibility ? View.VISIBLE : View.GONE);
    }


    @Override
    public void setTitle(int titleId) {
        mTv_title.setText(titleId);
    }

    public void setLeftIv(int id){
        mIv_left.setBackgroundResource(id);
    }
}
